* Table A5 - Impact of insurance on wages from agricultural labor
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if bal_panel3==1


* Creating winsorized ag labor revenue
replace rev_agrlabor=0 if rev_agrlabor==.
winsor rev_agrlabor, p(0.01) gen(rev_agrlabor_w1) highonly
gen rev_agrlabor_w1_real = rev_agrlabor_w1/deflator


gen tot_rev_new=totalrevenues_w1+rev_agrlabor_w1
gen tot_rev_new_real = tot_rev_new/deflator
gen profit_new = tot_rev_new-totalcosts_w1
gen profit_new_real=profit_new/deflator

* individual marketing treatments 
local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

* creating table

local table = 1
local rastring "replace"
local outcomevars "rev_agrlabor_w1_real profit_new_real"

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007  resurveyed lost_w3 , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 resurveyed lost_w3, fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed , fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../output/a5.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

** only for landless laborers

keep if plotsize_ha==0

local table = 1
local rastring "replace"
local outcomevars "rev_agrlabor_w1_real profit_new_real"

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007  resurveyed lost_w3 , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 resurveyed lost_w3 , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed , fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../output/a5b.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}
